public class StatePattern {
    public static void main(String[] args) {
        SchoolLife schoolLife=new SchoolLife();
        schoolLife.setState(new Forenoon());
        schoolLife.setHour(7);
        schoolLife.doSomething();
    }
}

interface State {
    void doSomething(SchoolLife sl);
}

class Forenoon implements State {

    @Override
   public void doSomething(SchoolLife sl) {
        if (sl.hour < 12 && sl.hour > 6) {
            System.out.println("sleep");
        } else {
            sl.state = new Afternoon();
            sl.doSomething();
        }
    }

}

class Afternoon implements State {

    @Override
    public  void doSomething(SchoolLife sl) {
        if (sl.hour >= 12 && sl.hour <18) {
            System.out.println("study");
        } else {
            sl.state = new Forenoon();
            sl.doSomething();
        }
    }

}

class SchoolLife {
    State state;
    int hour;

    void doSomething() {
        state.doSomething(this);
    }

    public State getState() {
        return state;
    }

    public void setState(State state) {
        this.state = state;
    }

    public int getHour() {
        return hour;
    }

    public void setHour(int hour) {
        this.hour = hour;
    }
}
